package com.ujiuye.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class DruidUtils {
    public static DataSource getDataSource(){
        InputStream input = DruidUtils.class.getClassLoader()
                .getResourceAsStream("druid.properties");
        Properties prop = new Properties();
        DataSource dataSource = null;
        try {
            prop.load(input);
            dataSource = DruidDataSourceFactory.createDataSource(prop);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }
/*
* 创建dbUtils 核心操作工具QueryRunner 对象，并传递了数据源
* @return QueryRunner 对象
* */
    public static QueryRunner getQueryRunner(){
        return new QueryRunner(getDataSource());
    }

    @Test
    public void test() throws SQLException {
        System.out.println(getDataSource().getConnection());
    }
}
